package com.tizona.dao.jdbc;

import com.sybase.jdbcx.SybDriver;

import java.sql.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SQLRequest
{


    public SQLRequest(Connection connection)
        throws Exception
    {

        con = null;
        dbref = null;
        con = connection;
        stmt = null;
        rs = null;

    }

    public void unlink()
    {
        con = null;
    }

    public SQLRequest(String s)
        throws Exception
    {

        con = null;
        dbref = null;
        SybDriver sybdriver = null;


        try
        {
            sybdriver = (SybDriver)Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
        }
        catch(Exception exception)
        {
            SybDriver sybdriver1;
            if((exception instanceof InstantiationException) || (exception instanceof ClassNotFoundException))
                sybdriver1 = (SybDriver)Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
            else
                throw exception;
        }
        logger.debug(s);
        dbref = s;
        con = DriverManager.getConnection(dbref);
    }

    public void doClose()
    {
        closeStmtAndRs();
        
        if(con != null)
            try
            {
                con.close();
            }
            catch(SQLException sqlexception) { }
        con = null;
    }

    public void finalize()
    {
        doClose();
    }

// didn't work out with autocommit on/off
/*
    public void commit() throws SQLException {
        con.commit();
        con.setAutoCommit(true);
    }
    public Savepoint setSavepoint(String savepoint) throws SQLException {
        con.commit();
        con.setAutoCommit(false);
        Savepoint svpt = con.setSavepoint(savepoint);
        logger.info("savepoint name:" + svpt.getSavepointName());
        logger.info("savepoint id:" + svpt.getSavepointId());
        return svpt;
    }

    public void rollbackToSavepoint(Savepoint savepoint) throws SQLException {
        con.rollback(savepoint);
        con.commit();
        con.setAutoCommit(true);
    }

    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        con.releaseSavepoint(savepoint);
        con.commit();
        con.setAutoCommit(true);
    }

*/

    public ResultSet query(String s) throws SQLException {
    try {
        stmt = con.createStatement();
        rs = stmt.executeQuery(s);
    } catch (SQLException e) {
        logger.error("Exception..." + e.getMessage());
        throw e;
    }
    return rs;
    }

    public void update(String s)
        throws SQLException
    {
        stmt = con.createStatement();
        try {
            stmt.executeUpdate(s);
        } catch (SQLException e) {
            logger.fatal(e.getMessage());
            throw e;
        } finally {
            try {
                stmt.close();
            } catch (SQLException ex) { /* just ignore */ }
        }
    }

    public FastStatement prepare(String s)
        throws SQLException
    {
        return new FastStatement(con.prepareStatement(s), s);
    }

    public FastStatement prepareSP(String s)
        throws SQLException
    {
        return new FastStatement(con.prepareCall(s), s);
    }

    public ResultSet query(FastStatement faststatement)
        throws SQLException
    {
        return faststatement.query();
    }

    public int update(FastStatement faststatement)
        throws SQLException
    {
        return faststatement.update();
    }

    public void closeStmtAndRs() {

        try {
            rs.close();
        } catch (SQLException e) {
//            e.printStackTrace();
        }
        try {
            stmt.close();
        } catch (SQLException e) {
//            e.printStackTrace();
        }
    }

    public Connection con;
    public Statement stmt;
    public ResultSet rs;
    public String dbref;
    public boolean prt_u;

    protected final Log logger = LogFactory.getLog(getClass());

}
